﻿using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SaiLing.Cache;
using SaiLing.Domain;
using SaiLing.Repository;
using SaiLing.SingleSignOn.Application.Core.Authorizations;
using SaiLing.SingleSignOn.Application.Service.Interfaces.System;
using SaiLing.SingleSignOn.Application.Service.Providers.System;
using SaiLing.SingleSignOn.Domain.Interfaces.System;
using SaiLing.SingleSignOn.Repository;
using SaiLing.SingleSignOn.Repository.Providers.System;
using SaiLing.Web;

namespace SaiLing.SingleSignOn.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.Configure<SaiLingSettings>(Configuration.GetSection("SaiLingSettings"));
            services.AddHttpContextAccessor()
                .AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                })
                .AddCookie(options =>
                {
                    options.LoginPath = "/Login";
                    options.Cookie.Domain = Configuration.GetSection("SaiLingSettings").GetSection("domain").Value;
                    options.Cookie.Name = Configuration.GetSection("SaiLingSettings").GetSection("token").Value;
                    options.Cookie.Path = "/";
                });
            services.AddAuthorization(options =>
            {
                options.AddPolicy("Web",
                    policy => policy.Requirements.Add(new WebAuthorizationRequirement()));
            });
            services.AddScoped(typeof(IDatabaseFactory<>), typeof(DatabaseFactory<>))
                .AddTransient<IAuthorizationHandler, WebAuthorizeHandler>()
                .AddTransient<ICacheService, MemoryCacheService>()
             .AddScoped<IUnitOfWork, UnitOfWork>()
             .AddScoped<IAccountRepository, AccountRepository>()
             .AddScoped<IAccountService, AccountService>()
             .AddScoped<IApplicationRepository, ApplicationRepository>()
             .AddScoped<IApplicationService, ApplicationService>()
             .AddScoped<IModuleRepository, ModuleRepository>()
             .AddScoped<IModuleService, ModuleService>()
             .AddScoped<IOperationLogRepository, OperationLogRepository>()
             .AddScoped<IOperationLogService, OperationLogService>()
             .AddScoped<IModuleActionRepository, ModuleActionRepository>()
             .AddScoped<IModuleActionService, ModuleActionService>()
             .AddScoped<IOrganizationService, OrganizationService>()
             .AddScoped<IOrganizationRepository, OrganizationRepository>()
              .AddScoped<IPositionService, PositionService>()
             .AddScoped<IPositionRepository, PositionRepository>()
              .AddScoped<IOrganizationMenuAuthorityService, OrganizationMenuAuthorityService>()
              .AddScoped<IOrganizationMenuAuthorityRepository, OrganizationMenuAuthorityRepository>();
            services.AddMemoryCache();
            services.AddMvc();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseAuthentication();
            app.UseCors(builder =>
            {
                builder.AllowAnyOrigin();
                builder.AllowAnyHeader();
                builder.AllowAnyMethod();
            });
            app.UseStaticHttpContext();
            app.UseStaticFiles();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
